﻿using RazorEngine;
using RazorEngine.Templating;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YiSha.SqlSugars
{
    public class CreateClassFile : IRazorService
    {
        /// <summary>
        /// 生成数据库实体类
        /// </summary>
        /// <param name="path">生成文件存储路径</param>
        /// <param name="namespaceName">命名空间</param>
        public static void GenerateEntityClass(string path)
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = SugarBase.DB_ConnectionString,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices()
                {//创建一个ConfigureExternalServices.cs 
                    RazorService = new CreateClassFile()
                }
            });
            //Razor模版进行修改
            var defaultTemplateString = DefaultRazorClassTemplate;
            db.DbFirst.UseRazorAnalysis(defaultTemplateString).CreateClassFile(path);
        }

        public List<KeyValuePair<string, string>> GetClassStringList(string razorTemplate, List<RazorTableInfo> model)
        {
            if (model != null && model.Any())
            {
                var result = new List<KeyValuePair<string, string>>();
                foreach (var item in model)
                {
                    try
                    {
                        item.ClassName = item.DbTableName;//这边可以格式化类名
                        string key = "RazorService.GetClassStringList" + razorTemplate.Length;
                        var classString = Engine.Razor.RunCompile(razorTemplate, key, item.GetType(), item);
                        var str = classString.Replace("?","");
                        result.Add(new KeyValuePair<string, string>(item.ClassName, str));
                    }
                    catch (Exception ex)
                    {
                        new Exception(item.DbTableName + " error ." + ex.Message);
                    }
                }
                return result;
            }
            else
            {
                return new List<KeyValuePair<string, string>>();
            }
        }
        public static string DefaultRazorClassTemplate = "using System;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing SqlSugar;\r\nnamespace @Model.Namespace \r\n{\r\n    ///<summary>\r\n    ///@Model.Description\r\n    ///</summary>\r\n    public partial class @Model.ClassName\r\n    {\r\n         public @(Model.ClassName)(){}\r\n @foreach (var item in @Model.Columns)\r\n   {\r\n      if(item.IsPrimarykey&&item.IsIdentity){\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>     \r\n         @:[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]      \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n        else if(item.IsPrimarykey)\r\n        {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>    \r\n         @:[SqlSugar.SugarColumn(IsPrimaryKey = true)]       \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         } \r\n        else if(item.IsIdentity)\r\n        {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>       \r\n         @:[SqlSugar.SugarColumn(IsIdentity = true)]    \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n         else\r\n         {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>           \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n       }\r\n\r\n    }\r\n}";
    }
}